home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / blf082b / killdupe.c < prev    next >
C/C++ Source or Header  |  1993-11-15  |  3KB  |  159 lines

  1. /*
  2.  * Detection of duplicate strings (Msg-ids for instance)
  3.  *
  4.  * Prototype:    int already_seen (char *string);
  5.  *
  6.  * Parameter:    a null-terminated string.
  7.  *
  8.  * Returns:    1 if the function "already_seen" has already been invoked
  9.  *        with the same string (case is sensitive). 0 otherwise.
  10.  *
  11.  * Side effects: adds "string" to the list of the already-seen strings,
  12.  *        provided that there is enough memory.
  13.  *
  14.  * Written by Thierry Bousch for the BloufGate project.
  15.  * Poorly updated to have memory other sessions by FA.
  16.  * This file is public domain.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <stddef.h>
  21. #include <string.h>
  22.  
  23. #include "blouf.h"    /* BLFSTR, f(blf)gets */
  24.  
  25. #define STRINGLEN BLFSTR
  26.  
  27. /*tb*/
  28.  
  29. #define HASHSIZE 512
  30.  
  31. typedef struct _string {
  32.     char *s;
  33.     struct _string *n;
  34. } String;
  35.  
  36. static int initialized = 0;
  37. static String *hash_table [HASHSIZE];
  38.  
  39. static int hash (char *string)
  40. {
  41.     unsigned long h = 0L;
  42.     char c;
  43.  
  44.     while ((c = *string++) != '\0')
  45.         h += (h << 1) + c;
  46.  
  47.     return (int)(h % HASHSIZE);
  48. }
  49.  
  50. /* check dupe */
  51.  
  52. int already_seen (char *string)
  53. {
  54.     int i;
  55.     String *p;
  56.  
  57.     if (!initialized) {
  58.         for (i = 0; i < HASHSIZE; i++)
  59.             hash_table[i] = NULL;
  60.         initialized = 1;
  61.     }
  62.     if (string == NULL)
  63.         return 1;    /* We know the (nil) string */
  64.     i = hash(string);
  65.     for (p = hash_table[i]; p != NULL; p = p->n)
  66.     {
  67.         if (strcmp(p->s, string) == 0)
  68.         {
  69.             return 1;    /* We know this string */
  70.         }
  71.     }
  72.     /* Otherwise add it to our list */
  73.     p = (String *)malloc(sizeof(String));
  74.     if (p != NULL) {
  75.         p->s = (char *)malloc(strlen(string)+1);
  76.         if (p->s) {
  77.             strcpy(p->s, string);
  78.             p->n = hash_table[i];
  79.             hash_table[i] = p;
  80.         } else    free(p);
  81.     }
  82.     return 0;    /* This was a new string to us */
  83. }
  84.  
  85. /*end/tb*/
  86.  
  87. static FILE *mccoy = NULL; /* file to put */
  88.  
  89. /*
  90.  * dupechk_open: dupe file load and initialize 
  91.  *    file:    name of the optional dupe file (NULL for none)
  92.  *    result: number of IDs loaded
  93.  */
  94.  
  95. long dupechk_open(char *file)
  96. {
  97.     long zebulon=0;
  98.     FILE *zork;
  99.     char kirk[STRINGLEN+1];
  100.  
  101.     if(!file)
  102.     {
  103.         mccoy=NULL;
  104.         return 0;
  105.     }    
  106.  
  107.     zork=fopen(file,"r");
  108.     if(zork)
  109.     {
  110.         while(fgets(kirk,STRINGLEN,zork)!=NULL)
  111.         {
  112.             if(*kirk)
  113.             {
  114.                 if(kirk[strlen(kirk)-1]<32)    /* kill \n */
  115.                     kirk[strlen(kirk)-1]=0;
  116.                 zebulon++;
  117.                 already_seen(kirk); /* add to database, easy way, probably poor performance */
  118.             }
  119.         }
  120.         fclose(zork);
  121.     }
  122.  
  123.     /* open to append next seen by */
  124.     mccoy=fopen(file,"a");
  125.  
  126.     return zebulon;
  127. }
  128.  
  129. /*
  130.  *    dupechk_seen: check if given ID has been seen, store otherwise
  131.  *    id:    message-id string
  132.  *    result: !0: yes, dupe; 0: not seen
  133.  */
  134.  
  135. int dupechk_seen(char *id)
  136. {
  137.     int ncc1701;
  138.  
  139.     ncc1701=already_seen(id);
  140.     if(!ncc1701)
  141.     { /*!seen*/
  142.         if(mccoy)
  143.             fputs(id,mccoy);
  144.     }    
  145.     return ncc1701;
  146. }
  147.  
  148. /*
  149.  * dupechk_close: close dupechecker, flush dupes file
  150.  */
  151.  
  152. void dupechk_close(void )
  153. {
  154.     if(mccoy)
  155.         fclose(mccoy);
  156.     mccoy=NULL;
  157. }
  158.  
  159. /*eof*/